"""客户类"""
from page.base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

class CustomPage(BasePage):

    url = '/crm5/index.php?m=customer'

    #定位器
    # 条件选择框
    select_option_locator = (By.CSS_SELECTOR, '#field')
    # 输入框
    input_text_locator = (By.ID, 'search')
    # 搜索按钮
    btn_search_locator = (By.ID, 'dosearch')
    # 查看
    contact_view_locator = (By.XPATH,
                            '//*[@id="form1"]/table/tbody/tr[1]/td[12]/a[1]')
    # 发送邮件
    send_email_locator = (By.CSS_SELECTOR, '#searchForm > ul > li:nth-child(5) > div:nth-child(2) > a')
    # 当前页发送
    current_page_locator = (By.CSS_SELECTOR, '#page_send_email')
    # 客户关怀
    contact_care_locator = (By.CSS_SELECTOR, 'body > div.container > div.page-header > ul > li:nth-child(4) > a')
    # 联系人
    contact_locator = (By.CSS_SELECTOR, 'body > div.container > div.page-header > ul > li:nth-child(3) > a')
    #新建客户按钮
    new_contact_locator = (By.CSS_SELECTOR,'body > div.container > div.row > div:nth-child(1) > div > a')
    #暂无数据文本
    no_data_locator = (By.CSS_SELECTOR, '#form1 > table > tbody > tr > td')


    def click_btn_option(self):
        """条件选择框"""
        select = Select(self.driver.find_element(*self.select_option_locator))
        select.select_by_index(1)

    def input_text(self, input_text):
        """输入关键字"""
        self.driver.find_element(*self.input_text_locator).send_keys(input_text)

    def click_btn_search(self):
        """点击搜搜"""
        self.driver.find_element(*self.btn_search_locator).click()

    def click_contact(self):
        """点击查看"""
        self.driver.find_element(*self.contact_view_locator).click()

    def click_send_email(self):
        """点击发送邮件"""
        self.driver.find_element(*self.send_email_locator).click()

    def click_send_current_page(self):
        """点击发送当前页"""
        self.driver.find_element(*self.current_page_locator).click()

    def click_contact_care(self):
        """点击客户关怀"""
        self.driver.find_element(*self.contact_care_locator).click()

    def click_contacts(self):
        """点击联系人"""
        self.driver.find_element(*self.contact_locator).click()

    def click_new_button(self):
        """点击新建客户"""
        self.driver.find_element(*self.new_contact_locator).click()

    def get_no_data_text(self):
        """获取暂无数据的文本"""
        return self.driver.find_element(*self.no_data_locator).text

    def get_search_results(self):
        """获取搜索结果表格中的所有行（tr元素）"""
        return self.driver.find_elements(By.XPATH,
                                         "//table[@class='table table-hover table-striped table_thead_fixed']/tbody/tr")

    def get_contact_names_from_results(self):
        """从搜索结果中提取所有联系人姓名"""
        rows = self.get_search_results()  # 获取所有表格行
        names = []
        for row in rows:
            # 从第二列（td[2]）中提取联系人姓名
            name_element = row.find_element(By.XPATH, "./td[2]//a")
            name = name_element.text.strip()
            names.append(name)
        return names



